package finalproject.model.rooms;

import finalproject.config.Randomizer;
import finalproject.model.items.ExamItem;
import finalproject.model.items.Item;
import finalproject.model.player.Player;
import finalproject.model.studies.Exam;
import finalproject.model.studies.Topic;
import finalproject.view.StringManager;

/**
 * The exam room
 * @author Nicolas Bighetti and Yassine Tijani
 *
 */
public class ExamRoom extends Room {
    
    /**
     * the exam 
     */
	private Exam exam;
	
	/**
	 * default constructor
	 * @param description the room description
	 * @param exam The associated exam
	 */
    public ExamRoom(String description, Exam exam) {
        super(description,true);
        this.exam = exam;
        // TODO Auto-generated constructor stub
    }

    /**
     * @return the exam
     */
    public Exam getExam()
    {
        return exam;
    }

    @Override
    public boolean doActivity(Player p) {
        if(exam == null)
        {
            return false;
        }
        if((this.exam.getTopic().equals(Topic.OOP)) && !(leavable(p)))
        {
            exam.doExam();
            if (exam.isPassed() || p.isCheater()) {
                StringManager.outPut("passed");
            }
            else
            {
                StringManager.outPut("failed");
            }
            return true; 
        }
        else
        {
            return false;
        }
    }

       @Override
       public boolean leavable(Player p) {
       if(this.exam == null)
       {
           return true;
       }
       for(Item item : p.getItemHolder().getHeldItems())
       {
           if(item instanceof ExamItem)
           {
               if(((ExamItem)item).getTopic().equals(this.exam.getTopic()))
               {
                   return true;
               }
           }
       }
       return false;
    }

	@Override
	public boolean prepareActivity(Player p) {
	    if(Randomizer.getRandom().nextInt(3) == 0)
	    {
	        this.exam = new Exam(Topic.OOP);
	        return true;
        }
	    else
	    {
	        this.exam = null;
	        return false;
	    }
	}

	@Override
	public String toString()
	{
	    if(this.exam == null)
	    {
	        return super.toString() + " : " + StringManager.getString("emptyroom");
	        
	    }
	    else
	    {
	        return super.toString() + " : " + StringManager.getString("oopexam");
	    }
	}



}
